package com.guigusuqi.hy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.guigusuqi.app.entity.LoginWxUser;
import com.guigusuqi.app.entity.DTO.UserCardInfoDTO;
import com.guigusuqi.app.entity.PO.UserCardInfo;
import com.guigusuqi.app.entity.WxUser;
import com.guigusuqi.app.service.WxUserService;
import com.guigusuqi.commonutils.utils.GetTokenByAuthorizationUtils;
import com.guigusuqi.commonutils.utils.RedisCache;
import com.guigusuqi.commonutils.vo.Result;
import com.guigusuqi.hy.entity.IntegralAdmin;
import com.guigusuqi.hy.entity.IntegralDetail;
import com.guigusuqi.hy.mapper.IntegralAdminMapper;
import com.guigusuqi.hy.service.IntegralAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigusuqi.hy.service.IntegralDetailService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 积分明细管理 服务实现类
 * </p>
 *
 * @author suqi
 * @since 2022-04-14
 */
@Service
public class IntegralAdminServiceImpl extends ServiceImpl<IntegralAdminMapper, IntegralAdmin> implements IntegralAdminService
{
    @Resource
    private RedisCache redisCache;

    @Resource
    private IntegralDetailService integralDetailService;

    @Resource
    private IntegralAdminMapper integralAdminMapper;

    @Resource
    private WxUserService wxUserService;

    @Override
    public Result addIntegral(String integralType, String openid) throws IOException {
        WxUser user = wxUserService.getUserInfoByOpenId(openid);
        String nickname = user.getNickname();
        String phone = user.getPhone();

        if (ObjectUtils.isEmpty(user) || StringUtils.isEmpty(phone))
        {
            return Result.fail().message("该用户不存在，或者该用户手机号为空！");
        }

        //通过积分类型获得要加的积分
        LambdaQueryWrapper<IntegralDetail> integralDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
        integralDetailLambdaQueryWrapper.eq(IntegralDetail::getIntegralType,integralType);
        IntegralDetail integralDetail = integralDetailService.getOne(integralDetailLambdaQueryWrapper);
        Integer count = integralDetail.getCount();
        String detail = integralDetail.getDetail();

        if (ObjectUtils.isEmpty(integralDetail))
        {
            return Result.fail().message("积分类型填写有误！");
        }
        // 获取卡号
        Result r = wxUserService.getUserCardInfo();

        UserCardInfoDTO userCardInfoDTO = (UserCardInfoDTO)r.getData().get("userCardInfo");
        IntegralAdmin integralAdmin = new IntegralAdmin();
        integralAdmin.setOpenid(openid);
        integralAdmin.setNickname(nickname);
        integralAdmin.setPhone(phone);
        integralAdmin.setIntegralNum(count);
        integralAdmin.setDetail(detail);
        Result result = null;
        if (!userCardInfoDTO.getIsMember()) // 不是会员
        {
            result = Result.success().message("不是会员,无法添加积分");
        }else if (userCardInfoDTO.getUserCardInfo().getIsUserCard()) // 是主卡用户
        {
            String cardNum = userCardInfoDTO.getUserCardInfo().getUserCard().getCardNum();
            integralAdmin.setCardNum(cardNum);
            int insert = integralAdminMapper.insert(integralAdmin);
            if (insert >= 1)
            {
                result = Result.success().message("主卡用户积分添加成功!");
            }
        }else // 只能是副卡用户
        {
            String cardNum = userCardInfoDTO.getUserCardInfo().getUserCard().getCardNum();
            integralAdmin.setCardNum(cardNum);
            int insert = integralAdminMapper.insert(integralAdmin);
            if (insert >= 1)
            {
                result = Result.success().message("副卡用户积分添加成功!");
            }
        }
        return result;
    }

    @Override
    public Result selectUserIntegral() throws IOException
    {
        String token = GetTokenByAuthorizationUtils.getToken();
        LoginWxUser loginWxUser = redisCache.getCacheObject("TOKEN_"+token);
        String openid = loginWxUser.getUser().getOpenid();

        UserCardInfoDTO userCardInfoDTO = (UserCardInfoDTO) wxUserService.getUserCardInfo().getData().get("userCardInfo");

        String cardNum = null;
        if (!userCardInfoDTO.getIsMember())
        {
            return Result.success().message("不是会员,没有积分记录");
        }else
        {
            if (userCardInfoDTO.getUserCardInfo().getIsUserCard()) // 是主卡,全部查出来
            {
                cardNum = userCardInfoDTO.getUserCardInfo().getUserCard().getCardNum();
                LambdaQueryWrapper<IntegralAdmin> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(IntegralAdmin::getCardNum,cardNum);
                List<IntegralAdmin> integralAdmins = integralAdminMapper.selectList(queryWrapper);
                return Result.success().data("rows",integralAdmins);
            }else // 是副卡，只查自己的积分
            {
                LambdaQueryWrapper<IntegralAdmin> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(IntegralAdmin::getOpenid,openid);
                List<IntegralAdmin> integralAdmins = integralAdminMapper.selectList(queryWrapper);
                return Result.success().data("rows",integralAdmins);
            }
        }
    }
}
